<?php
namespace Admin\Controller;

class InstallController extends BaseController{
	protected function _initialize(){
		if (file_exists(ROOT_PATH.'/Data/install.lock')) $this->failed('安装锁定，已经安装过了，如果您确定要重新安装，请到服务器上删除：./Data/install.lock。');
	}
	
	public function index(){
		if (IS_POST) $this->success(U(CONTROLLER_NAME.'/step2'));
		$this->display();
	}
	
	public function step2(){
		if (IS_POST){
			$Install = D('Install');
			$object = $Install->step2();
			if (is_numeric($object)){
				$output = '<?php';
				$output .= "\r\nreturn array(";
				$output .= "\r\n\t'DB_TYPE'=>'mysql',  //数据库类型";
				$output .= "\r\n\t'DB_HOST'=>'".I('db_host')."',  //数据库服务器";
				$output .= "\r\n\t'DB_PORT'=>'".I('db_port')."',  //服务器端口";
				$output .= "\r\n\t'DB_NAME'=>'".I('db_name')."',  //数据库名";
				$output .= "\r\n\t'DB_USER'=>'".I('db_user')."',  //数据库用户名";
				$output .= "\r\n\t'DB_PWD'=>'".I('db_pwd')."',  //数据库密码";
				$output .= "\r\n\t'DB_PREFIX'=>'".I('db_prefix')."',  //数据表前缀";
				$output .= "\r\n\t'DB_CHARSET'=>'UTF8',  //字符集";
				$output .= "\r\n\t'DB_PARAMS'=>array(\PDO::ATTR_CASE=>\PDO::CASE_NATURAL)  //区分大小写";
				$output .= "\r\n);\r\n?>";
				if (!file_put_contents(ROOT_PATH.'/App/Common/Conf/db.php',$output)) $this->failed('配置文件更新失败，请检查App/Common/Conf目录权限！');
				
				if (!mysql_connect(I('db_host'),I('db_user'),I('db_pwd'))) $this->failed('无法连接数据库，请重新设置数据库信息！');
				if (!mysql_query('CREATE DATABASE IF NOT EXISTS `'.I('db_name').'`;')) $this->failed('数据库创建失败！');
				mysql_select_db(I('db_name'));
				$sql = explode(';',str_replace('yjorder_',I('db_prefix'),file_get_contents(ROOT_PATH.'/Data/install.sql')));
				foreach ($sql as $value){
					if ($value) mysql_query($value) or $this->failed('数据库导入失败，请自行将Data目录下的install.sql导入到'.I('db_name').'数据库，然后<a href="'.U(CONTROLLER_NAME.'/step3').'">点击此处</a>进行下一步安装。',0,2);
				}
				mysql_close();
				
				$this->success(U(CONTROLLER_NAME.'/step3'));
			}else{
				$this->failed($object);
			}
		}
		$this->display();
	}
	
	public function step3(){
		if (IS_POST){
			$Install = D('Install');
			$object = $Install->step3();
			if (is_numeric($object)){
				$Manager = D('Manager');
				$object = $Manager->insert2();
				if (!is_numeric($object)) $this->failed($object);
				
				$output = '<?php';
				$output .= "\r\nreturn array(";
				$output .= "\r\n\t'WEB_NAME'=>'".str_replace("'",'"',I('post.web_name'))."',  //站点名称";
				$output .= "\r\n\t'SESSION_KEY'=>'".getKey(40)."',  //网站session key";
				$output .= "\r\n\t'ADMIN_MAIL'=>'".str_replace("'",'"',I('post.admin_mail'))."',  //管理员邮箱";
				$output .= "\r\n\t'URL_MODEL'=>'".I('post.url_model')."',  //URL模式";
				$output .= "\r\n\t'MANAGE_ENTER'=>'".str_replace("'",'"',I('post.manage_enter'))."',  //后台入口";
				$output .= "\r\n\t'MANAGE_SKIN'=>'default',  //后台皮肤";
				$output .= "\r\n\t'ORDER_DIR'=>'".str_replace("'",'"',I('post.order_dir'))."',  //下单页目录";
				$output .= "\r\n\t'ORDER_DB'=>'".I('post.order_db')."',  //订单入库";
				$output .= "\r\n\t'ORDER_TIME'=>'".I('post.order_time')."',  //防刷单间隔";
				$output .= "\r\n\t'ORDER_SEARCH'=>'0',  //订单查询";
				$output .= "\r\n\t'ORDER_SEARCH_STEP'=>'0',  //跨模板查询";
				$output .= "\r\n\t'ALIPAY_SERVICE'=>'create_partner_trade_by_buyer',  //支付宝支付接口";
				$output .= "\r\n\t'ALIPAY_SELLER'=>'',  //支付宝卖家账户";
				$output .= "\r\n\t'ALIPAY_PARTNER'=>'',  //支付宝合作身份";
				$output .= "\r\n\t'ALIPAY_KEY'=>'',  //支付宝安全校验码";
				$output .= "\r\n\t'TENPAY_PARTNER'=>'',  //财付通商户号";
				$output .= "\r\n\t'TENPAY_KEY'=>'',  //财付通密钥";
				$output .= "\r\n\t'WXPAY_APPID'=>'',  //微信支付APPID";
				$output .= "\r\n\t'WXPAY_MCHID'=>'',  //微信支付MCHID";
				$output .= "\r\n\t'WXPAY_KEY'=>'',  //微信支付KEY";
				$output .= "\r\n\t'WXPAY_APPSECRET'=>'',  //微信支付APPSECRET";
				$output .= "\r\n\t'YUNPAY_SELLER'=>'',  //云支付会员账户";
				$output .= "\r\n\t'YUNPAY_PARTNER'=>'',  //云支付合作身份";
				$output .= "\r\n\t'YUNPAY_KEY'=>'',  //云支付安全校验码";
				$output .= "\r\n\t'QQ_APPID'=>'',  //QQ互联APP ID";
				$output .= "\r\n\t'QQ_APPKEY'=>'',  //QQ互联APP KEY";
				$output .= "\r\n\t'QQ_VERIFY'=>'0',  //QQ互联审核模式";
				$output .= "\r\n\t'QQ_CODE'=>'',  //QQ互联验证代码";
				$output .= "\r\n\t'ORDER_MAIL_SUBJECT'=>'您有一笔新订单，请及时发货',  //订单提醒邮件标题";
				$output .= "\r\n\t'ORDER_MAIL_CONTENT'=>'<p>订单详情</p>
<p>订单编号：{oid}</p>
<p>订购产品：{proname}</p>
<p>成交单价：{proprice}元</p>
<p>订购数量：{procount}</p>
<p>成交总价：{prototal}元</p>
<p>姓　　名：{name}</p>
<p>联系电话：{tel}</p>
<p>详细地址：{province} {city} {county} {address}</p>
<p>邮政编码：{post}</p>
<p>备　　注：{note}</p>
<p>电子邮箱：{email}</p>
<p>　下单IP：{ip}</p>
<p>下单来路：{referrer}</p>
<p>支付方式：{pay}</p>',  //订单提醒邮件内容";
				$output .= "\r\n\t'PAY_MAIL_SUBJECT'=>'您的订单尚未支付，请及时支付',  //支付提醒邮件标题";
				$output .= "\r\n\t'PAY_MAIL_CONTENT'=>'<p>您的订单尚未支付，请及时支付，支付链接：<a href=\"{alipay}\" target=\"_blank\">支付宝支付</a>　<a href=\"{tenpay}\" target=\"_blank\">财付通支付</a>　<a href=\"{wxpay}\" target=\"_blank\">微信支付</a>　<a href=\"{yunpay}\" target=\"_blank\">云支付</a>。</p>
<p>订单详情</p>
<p>订单编号：{oid}</p>
<p>订购产品：{proname}</p>
<p>产品单价：{proprice}元</p>
<p>订购数量：{procount}</p>
<p>产品总价：{prototal}元</p>
<p>姓　　名：{name}</p>
<p>联系电话：{tel}</p>
<p>详细地址：{province} {city} {county} {address}</p>
<p>邮政编码：{post}</p>
<p>备　　注：{note}</p>
<p>电子邮箱：{email}</p>
<p>　下单IP：{ip}</p>
<p>下单来路：{referrer}</p>
<p>支付状态：{pay}</p>
<p>订单状态：{state}</p>
<p>下单时间：{date}</p>',  //支付提醒邮件内容";
				$output .= "\r\n\t'SEND_MAIL_SUBJECT'=>'您的订单已发货，请注意查收',  //发货提醒邮件标题";
				$output .= "\r\n\t'SEND_MAIL_CONTENT'=>'<p>您的物流信息为：物流公司 {loginame}　物流编号 {logiid}。您可以<a href=\"{logiurl}\" target=\"_blank\">点击此处</a>查询物流详情。</p>
<p>订单详情</p>
<p>订单编号：{oid}</p>
<p>订购产品：{proname}</p>
<p>产品单价：{proprice}元</p>
<p>订购数量：{procount}</p>
<p>产品总价：{prototal}元</p>
<p>姓　　名：{name}</p>
<p>联系电话：{tel}</p>
<p>详细地址：{province} {city} {county} {address}</p>
<p>邮政编码：{post}</p>
<p>备　　注：{note}</p>
<p>电子邮箱：{email}</p>
<p>　下单IP：{ip}</p>
<p>下单来路：{referrer}</p>
<p>支付状态：{pay}</p>
<p>订单状态：{state}</p>
<p>下单时间：{date}</p>'  //发货提醒邮件内容";
				$output .= "\r\n);\r\n?>";
			}else{
				$this->failed($object);
			}
			if (!file_put_contents(ROOT_PATH.'/App/Common/Conf/system.php',$output)) $this->failed('配置文件更新失败，请检查App/Common/Conf目录权限！');
			
			if (!rename(ROOT_PATH.'/admin.php',ROOT_PATH.'/'.I('post.manage_enter'))) $this->failed('后台入口重命名失败，请检查系统根目录权限！');
			if (!file_put_contents(ROOT_PATH.'/Data/install.lock',' ')) $this->failed('安装锁定写入失败，请检查Data目录权限！');;
			$this->success(C('WEB_URL').I('post.manage_enter'),'安装成功，即将跳转到后台登录页！');
		}
		$this->display();
	}
}